const app = getApp();

Page({
  data: {
    token: '',
    outline: false,
    hidden: false,

    code: '',
    deviceid: '',
  },

  onLoad(query) {
    // 页面加载


  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    var that = this;
    my.getNetworkType({
      success: (res) => {
        if (res.networkAvailable) {
          that.setData({
            outline: false,
          })
          // 获取token
          my.getStorage({
            key: 'token',
            success: function(res) {
              that.setData({
                token: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          my.getStorage({
            key: 'deviceid',
            success: function(res) {
              that.setData({
                deviceid: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
                 //获取openid
          my.getStorage({
            key: 'openid',
            success: function(res) {
              that.setData({
                openid: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
        } else {
          that.setData({
            outline: true
          })
        }
      }
    })
  },
  // 输入体验码
  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    });
  },
  // 启动
  str() {
    var that = this;
    if (that.data.code == '') {
      my.showToast({
        duration: 1500,
        content: '体验码不能为空，请输入体验码!'
      });
      return;
    }

    that.setData({
      status1: 1,
    })
    var req = {
      deviceid: that.data.deviceid,
      openid:  that.data.openid,
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/senddevicemsg', // 该url是自己的服务地址
      method: 'POST',
      data: req,
      headers: {
        'x-access-token': this.data.token
      },
      success: (res) => {
        if (res.data.status == 200) {
          that.setData({
            hidden: true
          })
          that.startSetInter()
        } else {
          my.showToast({
            content: res.data.err_msg,
            duration: 1500
          });
        }



      },
      fail: (res) => {
        console.log('/Wxpay_callback:', res)
      },
    });
  },


  startSetInter: function() {
    var that = this;
    var req = {
      deviceid: that.data.deviceid,
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/GetEmpPrice', // 该url是自己的服务地址
      method: 'POST',
      data: req,
      headers: {
        'x-access-token': this.data.token
      },
      success: (res) => {// 成功获取订单号
        if (res.data.device.status == 1) {
          that.pay();
        } else {
          that.data.status1++;
          if (that.data.status1 < 20) {
            setTimeout(function() {
              that.startSetInter();
            }, 1500)
          } else {
            that.setData({
              hidden: false,
              status1: 1
            })
            my.showToast({
              content: '设备正在联网，请5秒后扫码重试。。。',
              duration: 2000
            })
            return;
          }
        }
      },
      fail: (res) => {
        that.setData({
          hidden: false,
        })
        console.log('/Wxpay_callback:', res)
      },
    });
  },

  pay: function() {
    var that = this;
    var req = {
      deviceid: that.data.deviceid,
      TryCode: that.data.code
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/devicetest', // 该url是自己的服务地址
      method: 'POST',
      data: req,
      headers: {
        'x-access-token': this.data.token
      },
      success: (res) => {// 成功获取订单号
        that.setData({
          hidden: false,
        })
        my.showToast({
          content: '' + res.data.message + '',
          duration: 2000,
        })
      },
      fail: (res) => {
        console.log('/Wxpay_callback:', res)
      },
    });
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },


  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },

});
